package com.rongshu.view.penpal.activity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.home.activity.PreviewImgActivity;
import com.rongshu.view.penpal.present.PenpalHomePresenter;
import com.rongshu.view.penpal.bean.PenpalDetailsResponse;
import com.rongshu.view.penpal.view.PenpalHomeView;
import com.rongshu.widget.RoundImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2018/11/6.
 * description：笔友会主页
 */
public class PenpalHomeActivity extends BaseActivity<PenpalHomePresenter> implements PenpalHomeView {

    @BindView(R.id.head_middle)
    TextView headMiddle;
    @BindView(R.id.head_right)
    TextView headRight;
    @BindView(R.id.penpal_logo)
    RoundImageView penpalLogo;
    @BindView(R.id.penpal_name)
    TextView penpalName;
    @BindView(R.id.lable_content)
    LinearLayout lableContent;
    @BindView(R.id.city_content)
    TextView cityContent;
    @BindView(R.id.penpal_indrotuce)
    TextView penpalIndrotuce;
    @BindView(R.id.head_left)
    RelativeLayout headLeft;
    @BindView(R.id.head_content)
    RelativeLayout headContent;
    @BindView(R.id.left_iocn)
    ImageView leftIocn;
    @BindView(R.id.left_content)
    TextView leftContent;
    @BindView(R.id.left_option)
    RelativeLayout leftOption;
    @BindView(R.id.right_iocn)
    ImageView rightIocn;
    @BindView(R.id.right_content)
    TextView rightContent;
    @BindView(R.id.right_option)
    RelativeLayout rightOption;
    private int penpalwill_id = 0;
    private PenpalDetailsResponse.ResponseBean.ModelBean model;
    private int EDIT_PENPAL_MEG_REQUEST = 101;

    @Override
    protected int initLayout() {
        return R.layout.activity_penpal_home;
    }

    @Override
    protected void initView() {
        headMiddle.setText("笔友会详情");
        headLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        penpalwill_id = intent.getIntExtra(Constant.PENPAL_ID, 0);
        setPresenter(new PenpalHomePresenter(this), this);
        presenter.getPenpalDetails(penpalwill_id);
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick({R.id.head_right, R.id.right_option, R.id.left_option, R.id.penpal_logo})
    public void onViewClicked(View view) {
        if (model == null){
            return;
        }
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.head_right:
                if (model.getCurrentUserRank() == 1) {
                    intent.setClass(this, EditPenpalActivity.class);
                    intent.putExtra(Constant.PENPAL_ID, model.getPenpalwill_id());
                    startActivityForResult(intent, EDIT_PENPAL_MEG_REQUEST);
                }
                break;
            case R.id.right_option:
                intent.setClass(this, MemberManagerActivity.class);
                intent.putExtra(Constant.PENPAL_ID, penpalwill_id);
                if (model.getCurrentUserRank() == 0 || model.getCurrentUserRank() == 3) {//未加入||普通成员
                    intent.putExtra(Constant.PENPAL_MEMBER_OPTION, 0);
                } else {
                    intent.putExtra(Constant.PENPAL_MEMBER_OPTION, 1);
                }
                startActivity(intent);
                break;
            case R.id.left_option://副会长和普通成员点击该选项 退出笔友会,未加入成员点击 申请加入
                if (model != null && model.getCurrentUserRank() != 0) {
                    presenter.confirmExitPenpal(penpalwill_id);
                } else {
                    presenter.requestAddPenpal(penpalwill_id);
                }
                break;
            case R.id.penpal_logo:
                if (!TextUtils.isEmpty(model.getImg())){
                    ArrayList<String> listA = new ArrayList<>();
                    listA.add(model.getImg());
                    intent.setClass(this, PreviewImgActivity.class);
                    intent.putStringArrayListExtra(Constant.INTENT_DATA, listA);
                    startActivity(intent);
                } else {
                    toast("笔友会还没有设置过头像");
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == EDIT_PENPAL_MEG_REQUEST) {
                presenter.getPenpalDetails(penpalwill_id);
            }
        }
    }

    @Override
    public void callBackPenpalMsg(PenpalDetailsResponse response) {
        if (response != null && response.getResponse() != null) {
            model = response.getResponse().getModel();
            if (model != null) {
                GlideUtils.loadImageView(this, penpalLogo, model.getImg(), R.drawable.penpal_default_logo, R.drawable.penpal_default_logo);
                penpalName.setText(model.getPenclubname());
                cityContent.setText(model.getDistrict());
                penpalIndrotuce.setText(model.getSignature());
                switch (model.getCurrentUserRank()) {
                    case 1://会长
                        headRight.setText("编辑");
                        headRight.setTextColor(ContextCompat.getColor(this, R.color.color_3CC051));
                        leftOption.setVisibility(View.GONE);
                        break;
                    case 2://副会长
                        headRight.setVisibility(View.GONE);
                        leftIocn.setBackgroundResource(R.drawable.penpal_option_exit);
                        leftContent.setText("退出笔友会");
                        break;
                    case 3://普通成员
                        headRight.setVisibility(View.GONE);
                        leftIocn.setBackgroundResource(R.drawable.penpal_option_exit);
                        leftContent.setText("退出笔友会");
                        break;
                    default://未加入
                        headRight.setVisibility(View.GONE);
                        leftContent.setText("申请加入");
                        break;
                }
            }
            List<PenpalDetailsResponse.ResponseBean.LabelListBean> labelList = response.getResponse().getLabelList();
            lableContent.removeAllViews();
            if (labelList != null) {//动态添加标签
                for (int i = 0; i < labelList.size(); i++) {
                    TextView textView = new TextView(this);
                    textView.setText(labelList.get(i).getTitle() + "  ");
                    textView.setTextColor(ContextCompat.getColor(this, R.color.color_4a4a4a));
                    lableContent.addView(textView);
                }
                if (labelList.size() == 0) {//避免没有数据造成的页面展示异常
                    TextView textView = new TextView(this);
                    textView.setText("  ");
                    textView.setTextColor(ContextCompat.getColor(this, R.color.color_4a4a4a));
                    lableContent.addView(textView);
                }
            }
        }
    }

    @Override
    public void callBackJoinRequest() {
        if (model.getRatify()) {//判断加的笔友会不需要审核
            toast("申请提交成功\n正在进⾏审核");
            leftOption.setVisibility(View.VISIBLE);
        } else {
            toast("申请加入成功");
            leftOption.setVisibility(View.GONE);
        }
    }

    @Override
    public void callBackExitPenpal() {
        finish();
    }
}
